﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace BIExtAuth.DBUtility
{
    /// <summary>
    /// ClassName：SSAS操作类
    /// Author：scc-John
    /// CreateDate:2015-12-19
    /// </summary>
    public class SSASDBHelper : IDisposable
    {
        /// <summary>
        /// SSAS 服务器连接字符串
        /// </summary>
        private string asServerConnString = string.Empty;

        /// <summary>
        /// SSAS server对象
        /// </summary>
        private Microsoft.AnalysisServices.Server asServer = null;

        /// <summary>
        /// 实例化SSAS Server对象
        /// </summary>
        public SSASDBHelper()
            : this(ConfigurationManager.ConnectionStrings["ASDefaultServerConnString"].ToString())
        {

        }

        /// <summary>
        /// 实例化SSAS Server对象
        /// </summary>
        /// <param name="connString">自定义的SSAS服务连接字符串</param>
        public SSASDBHelper(string connString)
        {
            this.asServerConnString = connString;
            this.asServer = new Microsoft.AnalysisServices.Server();
            this.asServer.Connect(this.asServerConnString);
        }

        /// <summary>
        /// 根据数据库名称查找AS数据库
        /// </summary>
        /// <param name="name">AS数据库名称</param>
        /// <returns>AS数据库对象</returns>
        public Microsoft.AnalysisServices.Database GetDatabaseByName(string name)
        {
            Microsoft.AnalysisServices.Database db = null;
            db = this.asServer.Databases.FindByName(name);
            return db;
        }

        /// <summary>
        /// 根据数据库ID查找AS数据库
        /// </summary>
        /// <param name="dbid">AS数据库ID</param>
        /// <returns>AS数据库对象</returns>
        public Microsoft.AnalysisServices.Database GetDatabaseByID(string dbid)
        {
            Microsoft.AnalysisServices.Database db = null;
            db = this.asServer.Databases.Find(dbid);
            return db;
        }

        /// <summary>
        /// 根据角色名称查找角色
        /// </summary>
        /// <param name="db">AS数据库</param>
        /// <param name="roleName">要查找的角色名称</param>
        /// <returns>AS数据库角色对象</returns>
        public Microsoft.AnalysisServices.Role GetRoleByName(Microsoft.AnalysisServices.Database db, string roleName)
        {
            Microsoft.AnalysisServices.Role role = null;
            if (db != null)
            {
                role = db.Roles.FindByName(roleName);
            }
            return role;
        }

        /// <summary>
        /// 更新role
        /// </summary>
        /// <param name="role">要更新的role对象</param>
        /// <returns>-1：更新失败，1：更新成功</returns>
        public int UpdateRole(Microsoft.AnalysisServices.Role role)
        {
            int ret = -1;
            try
            {
                if (role != null)
                {
                    role.Update();
                    ret = 1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ret;
        }

        /// <summary>
        /// 获取所有的AS数据库
        /// </summary>
        /// <returns>AS数据库列表</returns>
        public Microsoft.AnalysisServices.DatabaseCollection GetAllDatabase()
        {
            try
            {
                Microsoft.AnalysisServices.DatabaseCollection dbList = null;
                if (this.asServer != null)
                {
                    dbList = this.asServer.Databases;
                }
                return dbList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取AS数据库所有的Cube
        /// </summary>
        /// <param name="dbname">AS数据名称</param>
        /// <returns>所有Cube列表</returns>
        public Microsoft.AnalysisServices.CubeCollection GetAllCubeBydbname(string dbname)
        {
            try
            {
                Microsoft.AnalysisServices.CubeCollection cubeList = null;
                Microsoft.AnalysisServices.Database db = this.GetDatabaseByName(dbname);
                if (db != null)
                {
                    cubeList = db.Cubes;
                }
                return cubeList;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 获取AS数据库所有的角色
        /// </summary>
        /// <param name="dbname">AS数据名称</param>
        /// <returns>所有角色列表</returns>
        public Microsoft.AnalysisServices.RoleCollection GetAllRoleBydbname(string dbname)
        {
            try
            {
                Microsoft.AnalysisServices.RoleCollection roleList = null;
                Microsoft.AnalysisServices.Database db = this.GetDatabaseByName(dbname);
                if (db != null)
                {
                    roleList = db.Roles;
                }
                return roleList;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        /// <summary>
        /// 释放对象资源
        /// </summary>
        public void Dispose()
        {
            if (this.asServer != null)
            {
                this.asServer.Disconnect();
                this.asServer.Dispose();
            }
        }
    }
}
